package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.RRException;
import yf.mall.dao.LevelDao;
import yf.mall.dao.MallActDao;
import yf.mall.dao.MallTypeDao;
import yf.mall.dao.UserDao;
import yf.mall.entity.*;
import com.yf.response.R;
import yf.mall.enums.BannerTypeEnum;
import yf.mall.enums.MallActStatusEnum;
import yf.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/api/home")
public class ApiHomeController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private MallGoodsService mallGoodsService;

    @Autowired
    MallActDao mallActDao;
    @Autowired
    ShopService shopService;
    @Autowired
    MallTypeDao mallTypeDao;
    @Autowired
    GoodsQualificationService goodsQualificationService;
    @Autowired
    private LiveService liveService;
    @Autowired
    private CommentService commentService;

    //首页
    @GetMapping({"/banner/{type}", "/banner"})
    @IgnoreAuth
    public R banner(@PathVariable(required = false, name = "type") Integer type) {
        //中部活动图片
        List<BannerEntity> topAct = bannerService.findTopAct();

        Map<String, Object> map = new HashMap<>();
        map.put("link_id", type);
        List<BannerEntity> bannerEntities = bannerService.queryList(map);


        return R.ok(bannerEntities).putExtra("topAct", topAct);
    }

//    //产品 TODO 分页 隐藏字段
//    @GetMapping("/goods")
//    @IgnoreAuth
//    public R goods(int limit,int page) {
//        List<MallGoodsEntity> mallGoodsEntities = mallGoodsService.topList(limit,page);
//        return R.ok(mallGoodsEntities);
//    }

    @Autowired
    private UserDao userDao;

    @Autowired
    private LevelService levelService;
    @Autowired
    private SaleTypeInfoService saleTypeInfoService;

    //产品详情 TODO 隐藏字段
    @GetMapping("/goods/info/{id}")
    public R info(@LoginUserId Long userId, @PathVariable("id") Long goodsId) {
        MallGoodsEntity mallGoodsEntity = mallGoodsService.getDetail(goodsId);
        LevelEntity levelEntity = levelService.findByUserId(userId,mallGoodsEntity.getAuth());
        //是否可见
        if(mallGoodsEntity.getLevelName()!=null&&mallGoodsEntity.getLevelName().contains(levelEntity.getName())){
            throw new RRException("级别不够!");
        }
        //从活动获取绑定产品
       // MallActEntity mallActEntity = mallActDao.findByGoodsIdAndstatus(mallGoodsEntity.getId(), MallActStatusEnum.OPEN.getIndex());
        List<MallActEntity> actEntities = mallActDao.findByBindGoodsId(goodsId);
        SaleTypeInfoEntity saleType = saleTypeInfoService.findByGoodsId(goodsId);
        List<MallGoodsEntity> bindList = new ArrayList<>();
        if(actEntities.size() > 0){
            for (MallActEntity mallActEntity : actEntities) {
                if (mallActEntity != null && mallActEntity.getEnd().after(new Date())) {
                    Long actGoodsId = mallActEntity.getGoodsId();
                    //存在绑定产品
                    if (actGoodsId != null) {
                        UserEntity userEntity = userDao.get(userId);

                        MallGoodsEntity bindGoods = mallGoodsService.get(actGoodsId);
                        levelEntity = levelService.findByUserId(userEntity.getLevelId(),bindGoods.getAuth());
                        MallActEntity bindAct = mallActDao.findByGoodsIdAndstatus(actGoodsId, MallActStatusEnum.OPEN.getIndex());
                        // 限制了当前级别
                        if (levelEntity != null &&bindAct!=null&& bindAct.getLevel() != null && bindAct.getLevel().contains(levelEntity.getName())) {
                            bindGoods = null;
                        }
                        if (bindGoods != null) {
                            bindGoods.setBindNumber(mallActEntity.getBindGoodsQuantity());
                            if (mallActEntity.getFirstGoodsQuantity() != null && mallActEntity.getFirstGoodsQuantity() > 0) {
                                bindGoods.setFirstBindNumber(mallActEntity.getFirstGoodsQuantity());
                            }
                        }
                        if (bindGoods != null){
                            bindList.add(bindGoods);
                        }
                    }
                }
            }
        }

        //获取店铺
        ShopEntity shopEntity = null;
        if (mallGoodsEntity.getUserId() != null) {
            shopEntity = shopService.findByUserId(mallGoodsEntity.getUserId());
        }
        //获取类别
        List<Long> typeIds = mallTypeDao.findByGoodsId(goodsId);
        List<String> list = new ArrayList<>();
        if (typeIds != null && typeIds.size() > 0) {
            for (Long typeId : typeIds) {
                MallTypeEntity mallTypeEntity = mallTypeDao.get(typeId);
                if (mallTypeEntity != null) {
                    list.add(mallTypeEntity.getName());
                }
            }
        }
        //获取品牌名
        GoodsQualificationEntity entity = goodsQualificationService.findByGoodsId(goodsId);
        String brandName = null;
        if (entity != null) {
            brandName = entity.getBrandName();
        }
        //获取直播
        Long liveId = null;
        List<Long> lives = liveService.findByGoodsId(mallGoodsEntity.getId());
        if (lives != null && lives.size() > 0) {
            liveId = lives.get(0);
        }
        PageHelper.startPage(0, 2);
        //获取评论信息
        List<CommentEntity> comments = commentService.findByGoodsId(mallGoodsEntity.getId());
        if (comments != null && comments.size() > 0) {
            mallGoodsEntity.setComments(comments);
        }
        return R.ok(mallGoodsEntity).putExtra("bindList", bindList)
                .putExtra("shop", shopEntity).putExtra("typeName", list).putExtra("brandName", brandName)
                .putExtra("liveId", liveId).putExtra("saleType",saleType);
    }

}
